-- 专门针对 MM2 的交易监控脚本 (修复版)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local localPlayer = Players.LocalPlayer
local tradeLogs = {}
local activeTradeId = nil

-- 增强的日志函数
local function logEvent(eventType, data)
    local timestamp = os.date("%H:%M:%S")
    local logEntry = string.format("[%s][%s] %s", 
        timestamp, 
        eventType,
        type(data) == "table" and table.concat(data, ", ") or tostring(data))
    
    if activeTradeId then
        logEntry = logEntry .. " | TradeID: "..activeTradeId
    end
    
    table.insert(tradeLogs, logEntry)
    print(logEntry)
end

-- 安全监控交易系统
local function monitorTradeSystem()
    -- 1. 查找交易文件夹
    local tradeFolder = ReplicatedStorage:FindFirstChild("Trade")
    if not tradeFolder then
        logEvent("ERROR", "未找到 Trade 文件夹")
        return
    end

    -- 2. 监控已知的 MM2 交易事件
    local tradeEvents = {
        Request = {name = "RequestTrade", type = "RemoteEvent"},
        GetRequest = {name = "GetRequest", type = "RemoteFunction"},
        Accept = {name = "AcceptTrade", type = "RemoteEvent"},
        Decline = {name = "DeclineTrade", type = "RemoteEvent"},
        AddItem = {name = "AddItem", type = "RemoteEvent"},
        RemoveItem = {name = "RemoveItem", type = "RemoteEvent"},
        Confirm = {name = "ConfirmTrade", type = "RemoteEvent"}
    }

    for _, eventInfo in pairs(tradeEvents) do
        local eventObj = tradeFolder:FindFirstChild(eventInfo.name)
        if eventObj then
            if eventInfo.type == "RemoteEvent" and eventObj:IsA("RemoteEvent") then
                local oldFire = eventObj.FireServer
                eventObj.FireServer = function(self, ...)
                    local args = {...}
                    if eventInfo.name == "RequestTrade" and #args > 0 then
                        activeTradeId = args[1] -- 假设第一个参数是交易ID
                    end
                    logEvent("EVENT:"..eventInfo.name, args)
                    return oldFire(self, ...)
                end
            elseif eventInfo.type == "RemoteFunction" and eventObj:IsA("RemoteFunction") then
                local oldInvoke = eventObj.InvokeServer
                eventObj.InvokeServer = function(self, ...)
                    local args = {...}
                    logEvent("FUNC_CALL:"..eventInfo.name, args)
                    local result = oldInvoke(self, ...)
                    logEvent("FUNC_RETURN:"..eventInfo.name, result)
                    
                    -- 从 GetRequest 获取交易ID
                    if eventInfo.name == "GetRequest" and type(result) == "table" then
                        activeTradeId = result.TradeId or result.tradeId
                    end
                    
                    return result
                end
            end
        else
            logEvent("WARNING", "未找到交易对象: "..eventInfo.name)
        end
    end

    -- 3. 监控玩家属性中的交易ID
    localPlayer.Changed:Connect(function(property)
        if property == "CurrentTradeId" or property == "currentTradeId" then
            activeTradeId = localPlayer[property]
            logEvent("PROPERTY", property.." = "..tostring(activeTradeId))
        end
    end)
end

-- 打印交易记录
local function printTradeLogs()
    print("\n===== 交易记录 =====")
    for i, log in ipairs(tradeLogs) do
        print(i..". "..log)
    end
    print("共记录 "..#tradeLogs.." 条事件")
    print("当前交易ID: "..(activeTradeId or "无"))
    print("===================")
end

         -- 初始化监控
logEvent("SYSTEM", "MM2 交易监控脚本启动")
local success, err = pcall(monitorTradeSystem)
if not success then
    logEvent("ERROR", "初始化失败: "..err)
end

-- 快捷键绑定
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if not gameProcessed then
        if input.KeyCode == Enum.KeyCode.P then
            printTradeLogs()
        elseif input.KeyCode == Enum.KeyCode.R then
            tradeLogs = {}
            activeTradeId = nil
            logEvent("SYSTEM", "日志已重置")
        end
    end
end)